﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using Random = System.Random;

/// <summary>
/// 测试UnityEngine.UI RebuildLayout Queue
/// </summary>
[ExecuteInEditMode]
public class GenLayoutRebuild : MonoBehaviour
{
    public Transform rootTransform;
    public Text changeText;
    public Image changeImage;

    [Range(0.001f, 0.5f)] [FormerlySerializedAs("TickInterval")] [Header("定时器")]
    public float tickInterval = 1f;

    public Coroutine tickCoroutine;
    public bool isTryRunning;

    public enum ChangeStuff
    {
        ChangeText,
        Disable,
    }

    public ChangeStuff changeStuffType;

    public GameObject SlotScrollViewContent;
    public GameObject SlotPrefab;
    private void OnEnable()
    {
        if (rootTransform == null)
            rootTransform = transform;
    }

    [EasyButtons.Button]
    public void AttachLayoutElements()
    {
        var children = rootTransform.GetComponentsInChildren<Transform>();
        foreach (var chi in children)
        {
            if (chi.GetComponent<LayoutElement>() == null)
                chi.gameObject.AddComponent<LayoutElement>();
        }

    }

    [EasyButtons.Button]
    public void TryChangeText()
    {
        changeStuffType = ChangeStuff.ChangeText;
        if (tickCoroutine == null)
        {
            tickCoroutine = StartCoroutine(EverySecTick());
        }
    }

    [EasyButtons.Button]
    public void TryDisablText()
    {
        changeStuffType = ChangeStuff.Disable;
        if (tickCoroutine == null)
        {
            tickCoroutine = StartCoroutine(EverySecTick());
        }
    }



    [EasyButtons.Button]
    public void StopTry()
    {
        if (tickCoroutine != null)
            StopCoroutine(tickCoroutine);

        isTryRunning = false;
        tickCoroutine = null;
    }

    [EasyButtons.Button("测试所有子节点disable，样例太少")]
    public void TryAllDisable()
    {
        if (rootTransform.childCount == 0) return;

        var act = rootTransform.GetChild(0).gameObject.activeSelf;
        var trans = rootTransform.GetComponentsInChildren<Transform>(); //只能找到不隐藏的对象
        foreach (var tran in trans)
        {
            tran.gameObject.SetActive(!act);
        }
    }
    [EasyButtons.Button("填充ScrollView，测layout")]
    public void ShowScrollViewContent()
    {
        Cleanup();
        
        List<string> recipes = new List<string>();
        for (int i = 0; i < 20; i++)
        {
            recipes.Add("" + i);
        }

        foreach (string s in recipes)
        {
            GameObject go = Instantiate(SlotPrefab);
            go.transform.SetParent(SlotScrollViewContent.transform);
        }
    }

    IEnumerator EverySecTick()
    {
        while (true)
        {
            isTryRunning = true;
            if (changeText != null)
            {
                if (changeStuffType == ChangeStuff.ChangeText)
                {
                    string[] strs = {"fff", "bbb", "ccc"};
                    var rand = new Random();
                    int i = rand.Next(0, strs.Length);
                    changeText.text = strs[i];
                }
                else if (changeStuffType == ChangeStuff.Disable)
                {
                    changeText.gameObject.SetActive(!changeText.gameObject.activeSelf);
                }
            }

            yield return new WaitForSeconds(tickInterval);
        }

    }

    private void OnDestroy()
    {
        if (tickCoroutine != null)
            StopCoroutine(tickCoroutine);
    }

    void Cleanup()
    {
        if(SlotScrollViewContent!=null)
            for (int i = SlotScrollViewContent.transform.childCount - 1; i >= 0; i--)
            {
                DestroyImmediate(SlotScrollViewContent.transform.GetChild(i).gameObject);
            }
    }


}
